package com.hong.utils.executors;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/7/27 16:11
 * @Description:各种策略模式
 */
public class ExecutorServiceExample2 {
    public static void main(String[] args) throws InterruptedException {
//        testAbortPolicy();
//        testDiscardPolicy();
//        testCallerRunsPolicy();
        testDiscardOldestPolicy();


    }

    /**
     * 被拒绝的任务的处理程序，抛出一个 RejectedExecutionException 。
     * @throws InterruptedException
     */
    private static void testAbortPolicy() throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(1,2,300,TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),r->{
                Thread t  = new Thread(r);
                return  t;
        },new ThreadPoolExecutor.AbortPolicy());
        for (int i =0;i<3;i++){
            executorService.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            TimeUnit.SECONDS.sleep(1);
            executorService.execute(()->{
                System.out.println("x");
            });
        }
    }

    /**
     * 被拒绝的任务的处理程序静默地丢弃被拒绝的任务。
     * 使用DiscardPolicy 非常危险
     * @throws InterruptedException
     */
    private static void testDiscardPolicy() throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(1,2,300,TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),r->{
            Thread t  = new Thread(r);
            return  t;
        },new ThreadPoolExecutor.DiscardPolicy());
        for (int i =0;i<3;i++){
            executorService.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            TimeUnit.SECONDS.sleep(1);
            executorService.execute(()->{
                System.out.println("x");
            });
            System.out.println("=====================");
        }
    }

    /**
     * 一个被拒绝的任务的处理程序，直接在 execute方法的调用线程中运行被拒绝的任务，除非执行程序已被关闭，否则这个任务被丢弃。
     * @throws InterruptedException
     */
    public static void testCallerRunsPolicy() throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(1,2,300,TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),r->{
            Thread t  = new Thread(r);
            return  t;
        },new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i =0;i<3;i++){
            executorService.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            TimeUnit.SECONDS.sleep(1);
            executorService.execute(()->{
                System.out.println("x");
                System.out.println(Thread.currentThread().getName());
            });
            System.out.println("=====================");
        }
    }

    /**
     * 被拒绝的任务的处理程序，丢弃最旧的未处理请求，然后重试 execute ，除非执行程序被关闭，在这种情况下，任务被丢弃。
     * @throws InterruptedException
     */
    public static void  testDiscardOldestPolicy() throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(1,2,300,TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),r->{
            Thread t  = new Thread(r);
            return  t;
        },new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i =0;i<3;i++){
            executorService.execute(()->{
                try {
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println("I come from lambda.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            TimeUnit.SECONDS.sleep(1);
            executorService.execute(()->{
                System.out.println("x");
                System.out.println(Thread.currentThread().getName());
            });
            System.out.println("=====================");
        }
    }
}
